<?php 
@session_start();
require_once '../sub/init.php';
//if(is_mobile() && empty($submitok))header("Location: m_login.php");
define('ZEAI2',dirname(__FILE__).DIRECTORY_SEPARATOR);
require_once ZEAI.'sub/conn.php';
require_once ZEAI.'cache/config_adm.php';
$endip = getip();
$rt = $db->query("SELECT ipurl FROM ".__TBL_IP__." WHERE ipurl='$endip'");
if($db->num_rows($rt))json_exit(array('flag'=>0,'msg'=>'访问已被拒绝'));
if ($submitok == 'ajax_submit') {
	$chkflag = 1;
	if (str_len($uname) >20 || str_len($uname) <2 || str_len($pwd) > 20 || str_len($pwd) < 6 ) {
		$content ="用户名密码错误，登录失败";
		$chkflag = 0;
	}
	if (!ifint($v,"0-9","4")){
		$content ="验证码错误，请重试";
		$chkflag = 0;
	}
	if (trim($v) !== $_SESSION['ZEAI_CN__YZM']) {	
		$content ="验证码校对错误，请重试";
		$chkflag = 0;
	}
	if ($adm_loginnum > 5 )json_exit(array('flag'=>0,'msg'=>'你重复发送次数过多，请联系管理员'));
	if ($chkflag == 1){
		$uname = trimm($uname);
		$pwd   = md5(trim($pwd));
		$rt = $db->query("SELECT id,roleid,kind,truename,agentid,agenttitle,path_s FROM ".__TBL_ADMIN__." WHERE  username='$uname' AND password='$pwd' AND flag=1");
		if ($db->num_rows($rt)) {
			$db->query("UPDATE ".__TBL_ADMIN__." SET endtime=".ADDTIME.",endip='$endip',logincount=logincount+1 WHERE username='$uname'");
			$row = $db->fetch_array($rt,'num');
			$_SESSION["admuid"]   = $row[0];
			$roleid               = $row[1];
			$_SESSION["kind"]     = $row[2];
			$_SESSION["truename"] = $row[3];
			$_SESSION["agentid"]  = intval($row[4]);
			$_SESSION["agenttitle"]= $row[5];
			$_SESSION["path_s"]    = $row[6];
			$_SESSION["admuname"] = $uname;
			$_SESSION["admpwd"]   = $pwd;
			if ( !ifint($roleid) )callmsg("角色载入错误","-1");
			$rtD=$db->query("SELECT authoritylist,crmkind,title,sq_sh_bfb FROM ".__TBL_ROLE__." WHERE id=".$roleid);
			if ($db->num_rows($rtD)){
				$rowD = $db->fetch_array($rtD,'num');
				$_SESSION["authoritylist"] = $rowD[0];
				$_SESSION["title"] = dataIO($rowD[2],'out');
				$_SESSION["sq_sh_bfb"] = $rowD[3];
			}else{
				json_exit(array('flag'=>0,'msg'=>'角色载入错误'));
			}
			if ($_SESSION["kind"] == 'crm'){
				$_SESSION["crmkind"]  = $rowD[1];
				if(ifint($_SESSION["agentid"])){
					$row = $db->ROW(__TBL_CRM_AGENT__,"flag,areaid","id=".$_SESSION["agentid"],'num');
					if ($row[0]!=1)json_exit(array('flag'=>0,'msg'=>'门店【'.$_SESSION["agenttitle"].'】已停止服务，请用超级管理员帐号进入开启'));	
					$_SESSION["agent_areaid"]=$row[1];
				}else{
					json_exit(array('flag'=>0,'msg'=>'门店载入错误'));
				}
			}
			unset($_SESSION['ZEAI_CN__YZM']);
			if($loginkind=='m'){
				$str='(手机端)后台';
				$url= 'm_adm.php';
			}else{
				$str='(电脑端)后台';
				$url= './';
			}
			AddLog2($str.'《帐号密码登录》成功'.'【'.$_SESSION["admuname"].'（id:'.$_SESSION["admuid"].'）】IP：'.$endip);
			setcookie("adm_loginnum",0,time()+720000,"/",$_ZEAI['CookDomain']);
			json_exit(array('flag'=>1,'msg'=>'已登录','url'=>$url));
		} else {
			AddLog2($str.'《帐号密码登录》失败'.'【'.$uname.'】IP：'.$endip);
			setcookie("adm_loginnum",$adm_loginnum+1,time()+720000,"/",$_ZEAI['CookDomain']);
			json_exit(array('flag'=>0,'msg'=>'您的用户名密码错误，超过5次将自动锁定不能再试，当前：'.($adm_loginnum+1).'次'));
		}
	}else{
		json_exit(array('flag'=>$chkflag,'msg'=>$content));
	}
	exit;
}elseif($submitok == 'ajax_logincode'){
	$logincode = cdstr(32);
	$url = HOST.'/sub/creat_ewm.php?url='.$_ZEAI['adm2'].'/get_adm_opneid.php?logincode='.$logincode;
	json_exit(array('flag'=>1,'logincode'=>$logincode,'url'=>$url));
}elseif($submitok == 'ajax_ewmlogin'){
	if(str_len($logincode)!=32)json_exit(array('flag'=>0));
	$row = $db->ROW(__TBL_TMP__,"openid","logincode='".$logincode."'","num");
	if ($row){
		$cook_adm_openid = $row[0];
		if(str_len($cook_adm_openid)>20){
			$row = $db->ROW(__TBL_ADMIN__,"id,roleid,kind,truename,agentid,agenttitle,path_s,username,password","openid<>'' AND openid='$cook_adm_openid'");
			if ($row){
				$db->query("UPDATE ".__TBL_ADMIN__." SET endtime=".ADDTIME.",endip='$endip',logincount=logincount+1 WHERE id=".$row[0]);
				$_SESSION["admuid"]   = $row[0];
				$roleid               = $row[1];
				$_SESSION["kind"]     = $row[2];
				$_SESSION["truename"] = $row[3];
				$_SESSION["agentid"]  = intval($row[4]);
				$_SESSION["agenttitle"]= $row[5];
				$_SESSION["path_s"]    = $row[6];
				$_SESSION["admuname"] = $row[7];
				$_SESSION["admpwd"]   = $row[8];
				if ( !ifint($roleid) )json_exit(array('flag'=>2,'msg'=>'角色载入错误'));
				$rtD=$db->query("SELECT authoritylist,crmkind,title,sq_sh_bfb FROM ".__TBL_ROLE__." WHERE id=".$roleid);
				if ($db->num_rows($rtD)){
					$rowD = $db->fetch_array($rtD,'num');
					$_SESSION["authoritylist"] = $rowD[0];
					$_SESSION["title"] = dataIO($rowD[2],'out');
					$_SESSION["sq_sh_bfb"] = $rowD[3];
				}else{
					json_exit(array('flag'=>2,'msg'=>'角色载入错误'));
				}
				if ($_SESSION["kind"] == 'crm'){
					$_SESSION["crmkind"]  = $rowD[1];
					if(ifint($_SESSION["agentid"])){
						$row = $db->ROW(__TBL_CRM_AGENT__,"flag,areaid","id=".$_SESSION["agentid"],'num');
						if ($row[0]!=1)json_exit(array('flag'=>0,'msg'=>'门店【'.$_SESSION["agenttitle"].'】已停止服务，请用超级管理员帐号进入开启'));	
						$_SESSION["agent_areaid"]=$row[1];
					}else{
						json_exit(array('flag'=>2,'msg'=>'门店载入错误'));
					}
				}
				$db->query("DELETE FROM ".__TBL_TMP__." WHERE logincode='$logincode'");
				$url= './';
				AddLog2($str.'(电脑端)后台《微信扫码登录》成功'.'【'.$_SESSION["admuname"].'（id:'.$_SESSION["admuid"].'）】IP：'.$endip);
				json_exit(array('flag'=>1,'msg'=>'登录成功','url'=>$url));
			}else{
				json_exit(array('flag'=>2,'msg'=>'您当前微信还没有绑定后台帐号<br>请联系管理员进行相关帐号绑定'));
			}
		}
		json_exit(array('flag'=>0));
	}else{
		$db->query("INSERT INTO ".__TBL_TMP__."(logincode,addtime) VALUES ('$logincode',".ADDTIME.")");
	}
	json_exit(array('flag'=>0));
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $_ADM['admSiteName']; ?></title>
<link href="css/login.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<meta name="generator" content="Zeai.cn V8" />
<style>
.tablee td.tbg{background-color:<?php echo $_ZEAI['mob_mbcolor'];?>;background-image:url("<?php echo RES;?>m/img/my_bg.png");background-size:100%}
.loginbtn{background-color:<?php echo $_ZEAI['mob_mbcolor'];?>;box-shadow:0 3px 8px rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.4);}
</style>
</head>
<body>
<div id="mask_box" class="alpha0_100"></div><table border="0" align="center" cellpadding="5" cellspacing="0" class="tablee animattime_fast fadeInDown">
<form name="WWWzeaiCN" method="post" action="login.php" onSubmit="return chkform()" autoComplete="off">
<tr><td height="110" align="center" class="tbg"><div class="title"><?php echo $_ADM['admSiteName']; ?>・管理系统<?php echo $_ZEAI['ver'];?></div></td></tr>
<tr><td height="20" align="left"></td></tr>
<tr>
<td height="55" align="center"><i class="ico">&#xe645;</i><input name="uname" id="uname" class="login" size="35" maxlength="20" autoComplete="off" placeholder="请输入登录帐号" /></td>
</tr>
<tr>
<td height="55" align="center"><i class="ico">&#xe620;</i><input name="pwd" id="pwd"  type="password" class="login" size="35" maxlength="20" autoComplete="off" placeholder="请输入登录密码"  /></td>
</tr>
<tr>
<td height="55" align="center" class="verifybox"><i class="ico verify">&#xe6c3;</i><input name="login W240" id="verifycode" class="login"  maxlength="4" autocomplete="off" placeholder="请输入验证码"  /><img src="../sub/authcode.php" alt="看不清楚请点击刷新验证码" title="看不清楚请点击刷新验证码" name="gylverify" align="middle" id="gylverify" style="cursor : pointer;" onclick="ReloadCode()" /></td>
</tr>
<tr>
<td height="70" align="center" valign="bottom"><input type="submit" value="登 录" class="loginbtn" /></td>
</tr>
<tr>
<td align="center" valign="top" class="btmm">推荐1920*1080以上显示分辨率和谷歌浏览器体验最好</td>
</tr>
<tr>
<td align="center" valign="top">
<div class="otherlogin">
    <em class="weixin" onClick="zeai_cn__ewmLogin('ewmlogin');"><span class="ico">&#xe607;</span><font>微信扫码登录</font></em>
    <em class="mobadm" onClick="zeai_cn__ewmLogin('mobadm');"><span class="ico">&#xe627;</span><font>手机端后台</font></em>
</div>
</td>
</tr>
<input type="hidden" id="loginkind" value="p" />
</form>
</table>
<div id="loginewm" class="my-subscribe_box"><img id="Z_e___A___I__c___N">
<h3><span class="ico" style="color:#31C93C">&#xe607;</span> 请用微信扫码进入《电脑端后台》<br>必须登录帐号已绑定管理员或红娘微信号<br>必须正确配置好微信公众号相关参数</h3>
</div>
<div id="mobadm" class="my-subscribe_box"><img src="<?php echo HOST.'/sub/creat_ewm.php?url='.$_ZEAI['adm2'].'/m_login.php';?>">
<h3><span class="ico" style="color:#31C93C">&#xe607;</span> 请用微信扫码进入《手机端后台》<br>手机打开后，请输入后台帐号密码或微信一键登录<br>网址：<?php echo $_ZEAI['adm2'].'/m_login.php';?></h3>
</div>
</body>
</html>
<script src="js/login.js?<?php echo $_ZEAI['cache_str'];?>" ></script>
<?php
function AddLog2($c) {
	global $db,$_SESSION;
	$session_uname   = $_SESSION["admuname"];
	$session_kind    = $_SESSION["kind"];//adm,crm
	$session_agentid = intval($_SESSION["agentid"]);
	$session_agenttitle  = $_SESSION["agenttitle"];
	$kind=($session_kind=='crm')?2:1;$c=dataIO($c,'in',2000);
	$db->query("INSERT INTO ".__TBL_LOG__."  (username,kind,content,addtime,agentid,agenttitle) VALUES ('$session_uname',$kind,'$c',".ADDTIME.",$session_agentid,'$session_agenttitle')");
}
ob_end_flush();?>